/*
 * Copyright (c) 2009-2010 HIT Microelectronic Center
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Jin Yinghan
 */

#ifndef __CPU_EDGE_PERCEPTRON_PRED_HH__
#define __CPU_EDGE_PERCEPTRON_PRED_HH__

#include <vector>

#include "base/types.hh"
#include "config/the_isa.hh"
#include "arch/isa_traits.hh"
#include "params/DerivEdgeCPU.hh"

class DerivEdgeCPUParams;

template<class Impl>
class PerceptronBP
{
  public:
    typedef TheISA::BlockID BlockID;
    typedef TheISA::ExitID ExitID;

    PerceptronBP(DerivEdgeCPUParams *params,bool history);

    ~PerceptronBP();

    void reset();

    TheISA::ExitID lookup(BlockID blockID, Addr &block_PC, ThreadID tid);

    /** Non-speculative update the weight tables. */
    TheISA::ExitID update(BlockID blockID, Addr &block_PC, ExitID exitID);

    /** Squash due to other reasons but branch misprediction. */
    void squash(BlockID blockID, ThreadID tid);

    /** Squash due to branch misprediction. */
    void squash(BlockID blockID, ExitID exitID,ThreadID tid);

    TheISA::ExitID lookupExit(Addr &block_PC, ThreadID tid);

    void speculativeUpdate(ThreadID tid, BlockID blockID,TheISA::ExitID exitID);

  private:
    typedef std::vector<int> WeightTableEntry;

    enum HistoryType {
        local,
        global
    };

    struct LFFEntry {
        unsigned index;
        unsigned long long history;
    };

    /** Lookup msb of exitID. */
    bool msbPredictorLookup(Addr &block_PC, unsigned long long history);

    /** Lookup lsb of exitID. */
    bool lsbPredictorLookup(Addr &block_PC, unsigned long long history);

    /** Lookup the middle bit of exitID. */
    bool mbPredictorLookup(Addr &block_PC, unsigned long long history);

    /** Non-speculative update the weight table of msb. */
    void updateMSBWeightTable(BlockID blockID, Addr &block_PC, ExitID exitID);

    /** Non-speculative update the weight table of lsb. */
    void updateLSBWeightTable(BlockID blockID, Addr &block_PC, ExitID exitID);

    /** Non-speculative update the weight table of the middle bit. */
    void updateMBWeightTable(BlockID blockID, Addr &block_PC, ExitID exitID);

    /** Generate exitID by the outputs of the three predictors. */
    TheISA::ExitID exitGen(bool msb, bool lsb, bool mb);

    /** Speculative update history using decoded exitID. */
    unsigned long long updateHistory(unsigned long long history, TheISA::ExitID exitID);

    /** Local history or global history. */   
    HistoryType historyType;

    /** The number of entries in local history table. */
    unsigned long long localHistoryTableSize;

    /** The mask for PC indexing local history table. */
    unsigned long long localHistoryPCMask;

    /** Array of local future file. */
    std::vector<LFFEntry> localFutureFile;

    /** Histories of all threads, speculatively updated. */
    std::vector<unsigned long long> historyTable;

    /** Histories of all in-flight blocks. */
    std::vector<unsigned long long> historyFile;

    /** The number of bits of history for predicting msb of exitID. */
    unsigned msbHistoryBits;

    /** The size of weight table for msb. */
    unsigned long long msbWeightTableSize;

    /** The mask of PC in msb weight table. */
    unsigned long long msbPCMask;

    /** The mask of history for predicting msb of exitID. */
    unsigned long long msbHistoryMask;

    /** The number of bits of history for predicting lsb of exitID. */
    unsigned lsbHistoryBits;

    /** The size of weight table for lsb. */
    unsigned long long lsbWeightTableSize;

    /** The mask of PC in lsb weight table. */
    unsigned long long lsbPCMask;

    /** The mask of history for predicting lsb of exitID. */
    unsigned long long lsbHistoryMask;

    /** The number of bits of history for predicting the middle bit of exitID. */
    unsigned mbHistoryBits;

    /** The size of weight table for the middle bit. */
    unsigned  mbWeightTableSize;

    /** The mask of PC in mb weight table. */
    unsigned long long mbPCMask;

    /** The mask of history for predicting the middle bit of exitID. */
    unsigned long long mbHistoryMask;

    /** The weight table for msb. */
    std::vector<WeightTableEntry*> msbWeightTable;
    //int msbWeightTable[512][29];

    /** The weight table for lsb. */
    std::vector<WeightTableEntry*> lsbWeightTable;
    //int lsbWeightTable[512][59];

    /** The weight table for the middle bit. */
    std::vector<WeightTableEntry*> mbWeightTable;
    //int mbWeightTable[512][43];

    /** The number of bits of mask. */
    unsigned weightBits;

    /** The maximum value of weight. */
    int maxWeight;

    /** The minimum value of weight. */
    int minWeight;

    /** The number of bits to align. */
    unsigned blockShiftAmt;
};

#endif // __CPU_EDGE_PERCEPTRON_PRED_HH__
